<?php
require_once '../../library/config.php';
require_once '../library/functions.php';

checkUser();

$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
	
	case 'addSeminar' :
		addSeminar();
		break;
		
	case 'modifySeminar' :
		modifySeminar();
		break;
		
	case 'deleteSeminar' :
		deleteSeminar();
		break;
	
	case 'deleteImage' :
		deleteImage();
		break;
    

	default :
	    // if action is not defined or unknown
		// move to main Seminar page
		header('Location: index.php');
}


function addSeminar()
{
    $maht       = $_POST['txtMaht'];
	$tenht       = $_POST['txtTenht'];
	$chude		 = $_POST['txtChude'];
	$ngaythuchien = $_POST['txtNgaythuchien'];
	$diadiem = $_POST['txtDiadiem'];
	$truong_id = $_POST['cboSchool'];
	$dv_id		 = $_POST['cboDept'];
	$loaiht_id 	= $_POST['cboStyleSeminar'];
	//$nn_id       = $_POST['cbotopicGroup'];
	
	$arr = explode("-", $ngaythuchien);
	$ngaythuchien = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$sql3   = "INSERT INTO hoithao (maht, tenht, chude, ngaythuchien, diadiem, dv_id, loaiht_id)
	          VALUES ('$maht', '$tenht', '$chude','$ngaythuchien', '$diadiem', $dv_id, $loaiht_id)";

	$result3 = dbQuery($sql3);
	//echo $sql3; exit();
		
	header("Location: index.php");	
}

/*
	Modify a Seminar
*/
function modifySeminar()
{
	$id 		= (int)$_GET['id'];
	$maht       = $_POST['txtMaht'];
	$tenht       = $_POST['txtTenht'];
	$chude		 = $_POST['txtChude'];
	$ngaythuchien = $_POST['txtNgaythuchien'];
	$diadiem = $_POST['txtDiadiem'];
	$truong_id = $_POST['cboSchool'];
	$dv_id		 = $_POST['cboDept'];
	$loaiht_id 	= $_POST['cboStyleSeminar'];

	$arr = explode("-", $ngaythuchien);
	$ngaythuchien = $arr[2] . "-" . $arr[1] . "-" . $arr[0];
	
	$sql3   = "UPDATE hoithao
               SET maht = '$maht', tenht = '$tenht', chude = '$chude', ngaythuchien = '$ngaythuchien', diadiem = '$diadiem', dv_id = $dv_id, loaiht_id = $loaiht_id
			   WHERE id = $id" ;
    // echo $sql3;  
	$result3 = dbQuery($sql3);
	
	header('Location: index.php');			  
}

/*
	Remove a Seminar
*/
function deleteSeminar()
{
	if (isset($_GET['id']) && $_GET['id'] > 0) {
	$id = (int)$_GET['id'];
	} else {
		header('Location:index.php');
	}	
		
	$sql1 = "UPDATE hoithao SET hieu_luc = 0 WHERE id = $id"; 

	dbQuery($sql1);
		
    header('Location: index.php'); 
}
?>